package Basepractice.P2017_11_5;

import java.io.IOException;

import static java.lang.Double.isFinite;
import static java.lang.Double.max;

/**
 * Created by Administrator on 2017/11/5.
 */
public class Application {
    public static void main(String[] args) {
        char ch='5';
        int i,y,n,m,x;
        long sum,fact,p;
        while(ch!='0'){
            System.out.println("1.输出1+2+...+n的结果");
            System.out.println("2.输出1*2*...*n的结果");
            System.out.println("3.输出两个随机整数的最大值");
            System.out.println("4.求使得1+2+...+n的值大于p的最小n值");
            System.out.println("5.求m和n的最大公约数和最小公倍数");
            System.out.println("0.退出系统");
            System.out.println("选择[0，1，2，3，4，5]");
            try {
               ch=(char)System.in.read();        //接受用户输入的一个字符
            }catch (IOException e){
                System.out.println(e);     //输出异常信息
            }
            switch (ch){
                case '1':
                    n=(int)(Math.random()*100);
                    System.out.println("sum(1.."+n+")="+sum(n));
                    break;
                case '2':
                    n=n=(int)(Math.random()*20);
                    System.out.println(n+"!="+fact(n));
                    break;
                 case '3':
                    x=(int)(Math.random()*100);
                    y=(int)(Math.random()*100);
                    System.out.println("max("+x+","+y+")="+max(x,y));
                    break;
                case '4':
                    p=100+(int)(Math.random()*10000);
                    System.out.println("(1+2+...+n)>="+p+"的最小n="+minN(p));
                    break;
                case '5':
                    m=10+(int)(Math.random()*1000);
                    n=10+(int)(Math.random()*1000);
                    System.out.println(m+"和"+n+"的最大公约数="+max1(n,m));
                    System.out.println(m+"和"+n+"的最小公约数="+m*n/max1(n,m));
                    break;
                case '0':
                    System.out.println("退出系统");
                    break;
            }
        }
    }
    public static  long sum(int n){
        long sum1=0;
        int i;
        for (i=1;i<=n;i++){
            sum1=sum1+i;}
        return sum1;
    }
    public static long fact(int n){
        long fact1=1;
        int i;
        for (i=1;i<=n;i++){
            fact1=fact1*i;}
            return fact1;
    }
    public static int max(int x,int y){
        return x>y?x:y;
    }

    public static int minN(long m){
        long sum=0;
        int n=0;
        while(sum<m){
            sum=sum+n;n=n+1;
        }return n;
    }
    public static int max1(int n,int m){
        int temp,r;
        if (n<m){temp=n;n=m;m=temp;}
        while(m!=0){
            r=n%m;
            n=m;
            m=r;
        }
        return n;
    }
}
